package org.ws.lifebacklog.util;

import org.ws.lifebacklog.exception.NotInitializedExeption;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Wrapper for {@link SharedPreferences} that retrieve and store application specific properties
 * 
 * @author sergey.valuy
 * */
public class AppConfig {

	private static AppConfig mConf;
	private SharedPreferences mPref;
	private Context mCtx;
	private static final String KEY_ACTIVE_TAB = "KEY_ACTIVE_TAB";
	private static final String KEY_FIRST_VISIT = "KEY_FIRST_VISIT";
	private static final String KEY_DEV_MODE = "KEY_DEV_MODE";
	private static final String KEY_DB_VERSION = null;

	public AppConfig(Context context) {
		mCtx = context;
		mPref = mCtx.getSharedPreferences("config", Context.MODE_PRIVATE);
	}

	public int getActiveTab() {
		return mPref.getInt(KEY_ACTIVE_TAB, 0);
	}

	public void setActiveTab(int currentTab) {
		mPref.edit().putInt(KEY_ACTIVE_TAB, currentTab).commit();
	}

	public boolean isDevMode() {
		return mPref.getBoolean(KEY_DEV_MODE, false);
	}

	public void setDevMode(boolean isFirstVisit) {
		mPref.edit().putBoolean(KEY_DEV_MODE, isFirstVisit).commit();
	}

	public boolean isFirstVisit() {
		return mPref.getBoolean(KEY_FIRST_VISIT, true);
	}

	public void setFirstVisit(boolean isFirstVisit) {
		mPref.edit().putBoolean(KEY_FIRST_VISIT, isFirstVisit).commit();
	}

	/**
	 * @return
	 */
	public String getLanguage() {
		return "en";
	}

	public static void init(Context context) {
		mConf = new AppConfig(context);
	}

	/**
	 * 
	 */
	public static AppConfig getInstance() {
		if (mConf == null) {
			throw new NotInitializedExeption("Config is not initialized.");
		}
		return mConf;
	}

	public int getDbVersion() {
		return mPref.getInt(KEY_DB_VERSION, 1);
	}

	/**
	 * @param dbVersion
	 */

	public void setDbVersion(int dbVersion) {
		mPref.edit().putInt(KEY_DB_VERSION, dbVersion).commit();
	}

}
